<?php
session_start();

// 如果已经登录，重定向到控制台
if(isset($_SESSION['admin'])) {
    header('Location: index.php');
    exit;
}

// 处理登录表单提交
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    include_once 'includes/db.php';

    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);

    // 简单的用户名密码验证（实际项目中应该使用更安全的验证方式）
    if($username == 'admin' && $password == 'admin123') {
        $_SESSION['admin'] = true;
        header('Location: index.php');
        exit;
    } else {
        $error = '用户名或密码错误';
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>优塑科技 - 后台登录</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/css/admin.css">
</head>
<body class="bg-light">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-6 col-lg-4">
                <div class="card o-hidden border-0 shadow-lg my-5">
                    <div class="card-body p-0">
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="p-5">
                                    <div class="text-center">
                                        <h1 class="h4 text-gray-900 mb-4">优塑科技后台</h1>
                                    </div>
                                    <?php if(isset($error)) { ?>
                                        <div class="alert alert-danger" role="alert">
                                            <?php echo $error; ?>
                                        </div>
                                    <?php } ?>
                                    <form method="post" action="login.php">
                                        <div class="mb-3">
                                            <label for="username" class="form-label">用户名</label>
                                            <input class="form-control" id="username" name="username" type="text" required>
                                        </div>
                                        <div class="mb-3">
                                            <label for="password" class="form-label">密码</label>
                                            <input class="form-control" id="password" name="password" type="password" required>
                                        </div>
                                        <div class="d-grid gap-2">
                                            <button class="btn btn-primary btn-user" type="submit">登录</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>